define(function(require, exports, module) {
    var router = require("router");
    //var app = require("app");
    var controller = {};
    controller.vue = function() {
        controller.vm = new Vue({
            router: router,
            data: function() {
                return {
                    items: [],
                    store:{},
                    nodata:0,
                    loading:0,
                    title:'',
                    commission_money:''
                    
                };
            },
            el: '#pageIndex',
            created: function() {
                this.fetchData();
            },
            methods: {
                fetchData: function() {
                    var _this = this;
                    this.getList();
                },
                addCard:function(){
                  app.show_popup($("#popup_choose_wx"));
                },
               
               pageShow:function(){
                    var reload = app.getPageData("index","reload");
                    var that = this;
                    if(reload == 1){
                       app.setPageData("index",{reload:0});
                       that.getList(1);
                    }
                },
                setCommission:function($event){
                  console.log($event);
                  var _this = $($event.target);
                  var that = this;
                  var title = _this.attr("data-title");
                  that.title = title;
                  that.target = _this;
                  that.id = _this.attr("data-idx");
                  var commission_money = _this.attr("data-commission_money");
                  if(commission_money>0){
                     that.commission_money=commission_money;
                  }
                  app.show_popup("#popup_commission");
                },

                saveCommission:function(){
                  var that = this;
                   app.get("apiv2/wx_card/add", {
                      id : that.id,
                      commission_money:$("#commission_money").val(),
                    }, function (res) {
                      that.loading = 0;
                      if (res.code != 200) {
                         app.alert(res.msg);
                         return ;
                      }
                      $( that.target).text($("#commission_money").val()+"%");
                      app.hide_popup("#popup_commission");
                    });
                },
                // more:function(e){
                //   // console.log(_this);
                //   var _this = $(e.target);
                //   var that = this;
                //   var id = _this.attr("data-id");
                //   var appid = _this.attr("data-appid");
                //   var card_id = _this.attr("data-card_id");
                //   var params=[
                //     {
                //       'text':'修改',
                //       'methods':function(){
                //            app.colseHandle();
                //            that.$router.push({ name: 'addMemberCard', params: { id: id,appid:appid } });
                //       }
                //     },
                //     {
                //       'text':'删除',
                //       'methods':function(){
                //           app.colseHandle();
                //           that.delete(card_id,_this);
                          
                //       }
                //     }
                //   ];
                //   app.handle(params);

                //      // _this.$router.push({ name: 'addMemberCard', params: { id: _this.cardInfo. } });
                //      // { name: 'addMemberCard', params: { id: cardInfo.id ,appid:0 }}
                // },

                deleteCard:function(e){
                  var _this = $(e.target);
                  var that = this;
                  var id = _this.attr("data-id");
                  var appid = _this.attr("data-appid");
                  var card_id = _this.attr("data-card_id");
                  
                  app.confirm({
                     'text':'删除卡券后，该卡券对应已生成的领取用二维码、添加到卡包JS API均会失效。确认删除吗？',
                     'yes':function(){
                        app.get("apiv2/card/delete",{card_id:card_id},function(res){
                            if(res.code!=200){
                               app.alert(res.msg);
                               return ;
                            }
                            _this.parents(".item").hide();
                        });
                     }
                  });
                   
                },
                getList:function(reload){
                    var that = this;

                    if( that.loading == 1){
                        return ;
                     }
                     that.loading = 1;
                     
                    var user_info =  app.getUserInfo();
                    app.get("api/user/info", {user_id:user_info.user_id,fields:'store'},function(res){
                        that.store = res.data.store;
                    });
                    app.get("apiv2/wx_card/get", {
                      store_id : user_info.store_id,
                      limit:50
                    }, function (res) {
                      that.loading = 0;
                      if (res.code != 200) {
                         app.alert(res.msg);
                         return ;
                      }
                      that.items = res.data.list;
                      if(that.items.length===0){
                        that.nodata=1;
                      }else{
                        that.nodata=0;
                      }
                      
                    });
                }
            }

        }).$mount('#app');
    };
    module.exports = controller;
});
